package org.zachary.creditbusiness.commodityandhouse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zachary.creditbusiness.commodityandhouse.entity.CommodityOrder;

import org.zachary.creditbusiness.commodityandhouse.service.CommodityOrderService;
import org.zachary.creditbusiness.commodityandhouse.util.Result;
import org.zachary.creditbusiness.commodityandhouse.dto.OrderInitResponse;

import org.zachary.creditbusiness.commodityandhouse.dto.OrderInitResponse;
import org.zachary.creditbusiness.commodityandhouse.service.CommodityOrderService;
import org.zachary.creditbusiness.commodityandhouse.util.Result;


@RestController
@RequestMapping("/commodity-order")
public class CommodityOrderController {

    @Autowired
    private CommodityOrderService commodityOrderService;

    // 初始化订单
    @PostMapping("/init-order")
    public Result<OrderInitResponse> initOrder(@RequestParam Integer commodityId, @RequestParam Integer userId) {
        return commodityOrderService.initOrder(commodityId, userId);
    }

    // 下单
    @PostMapping("/place-order")
    public Result placeOrder(@RequestBody CommodityOrder commodityOrder) {
        return commodityOrderService.placeOrder(commodityOrder);
    }

    // 根据用户ID查询订单列表
    @GetMapping("/user/{userId}")
    public Result selectByUserId(@PathVariable Integer userId) {
        return commodityOrderService.selectByUserId(userId);
    }

    // 根据ID查询订单详情
    @GetMapping("/select/{id}")
    public Result selectById(@PathVariable Integer id) {
        return commodityOrderService.selectById(id);
    }

    // 更新订单状态
    @PutMapping("/update-status/{id}")
    public Result updateOrderStatus(@PathVariable Integer id, @RequestParam Byte status) {
        return commodityOrderService.updateOrderStatus(id, status);
    }

}
